/**
 * \file
 *
 * \brief I/O Ports implementation.
 *
 * Copyright (C) 2009 Atmel Corporation. All rights reserved.
 *
 * \page License
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * 3. The name of Atmel may not be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * 4. This software may only be redistributed and used in connection with an
 * Atmel AVR product.
 *
 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */
#ifndef GPIO_PORT_H_INCLUDED
#define GPIO_PORT_H_INCLUDED

#include <compiler.h>
#include <types.h>
#include <regs/xmega_port.h>

/**
 * \defgroup port_driver_group PORT Driver
 *
 * This is a driver implementation for the I/O ports peripheral. The I/O ports
 * peripheral can be found on XMEGA devices and it controls and configures the
 * I/O pins.
 * @{
 */

/**
 * \brief A bitmask representing a set of pins on a port
 *
 * Starting from pin 0 as the LSB, each bit in the mask corresponds to
 * a pin on some port. Each '1' bit includes the corresponding pin in
 * the set.
 */
typedef uint8_t	pin_mask_t;

/**
 * \brief A GPIO pin
 *
 * This type is used to describe the GPIO pins on the part.
 */
typedef uint16_t gpio_pin_t;

/**
 * \brief Pin configuration flags
 *
 * This is a bitmask containing configuration flags for the pins that shall be
 * configured.
 */
typedef uint16_t port_pin_flags_t;

//! \name Input/Output Configuration Flags
//@{
#define PORT_DIR_INPUT          (0 << 8) //!< Pin is Input
#define PORT_DIR_OUTPUT         (1 << 8) //!< Pin is Output
//@}

//! \name Initial Output State Flags
//@{
#define PORT_INIT_LOW           (0 << 9) //!< Initial Ouptput State is High
#define PORT_INIT_HIGH          (1 << 9) //!< Initial Ouptput State is Low
//@}

//! \name Input/Sense Configuration Flags
//@{
#define PORT_BOTHEDGES          (0 << 0) //!< Sense Both Edges
#define PORT_RISING             (1 << 0) //!< Sense Risign Edge
#define PORT_FALLING            (2 << 0) //!< Sense Falling Edge
#define PORT_LEVEL              (3 << 0) //!< Sense Low Level
#define PORT_INPUT_DISABLE      (7 << 0) //!< Input Buffer Disabled
//@}

//! \name Output and Pull Configuration Flags
//@{
#define PORT_TOTEM              (0 << 3) //!< Normal push/pull output
#define PORT_BUSKEEPER          (1 << 3) //!< Bus Keeper
#define PORT_PULL_DOWN          (2 << 3) //!< Pull-Down (when input)
#define PORT_PULL_UP            (3 << 3) //!< Pull-Up (when input)
#define PORT_WIRED_OR           (4 << 3) //!< Wired OR
#define PORT_WIRED_AND          (5 << 3) //!< Wired AND
#define PORT_WIRED_OR_PULL_DOWN (6 << 3) //!< Wired OR and Pull-Down
#define PORT_WIRED_AND_PULL_UP  (7 << 3) //!< Wired AND and Pull-Up
//@}

//! \name Inverted I/O Configuration Flags
//@{
#define PORT_INV_ENABLED        (1 << 6) //!< I/O is Inverted
#define PORT_INV_DISABLE        (0 << 6) //!< I/O is Not Inverted
//@}

//! \name Slew Rate Limit Configuration Flags
//@{
#define PORT_SRL_ENABLED        (1 << 7) //!< Slew Rate Limit Enabled
#define PORT_SRL_DISABLED       (0 << 7) //!< Slew Rate Limit Disabled
//@}


/**
 * \brief Create a GPIO number
 *
 * This macro creates a GPIO pin number from a pin on a port. The GPIO pin
 * number can be used with the functions provided from this driver.
 *
 * \param port Port name e.g. PORTA.
 * \param pin Pin number on the port, valid values are 0 to 7.
 */
#define CREATE_GPIO_PIN(port, pin) ((PORT_##port) + (pin))

/**
 * \brief Create a pointer to a port from a GPIO pin number
 *
 * It is assumed that all ports are lined up after PORTA in the memory map like
 * it is described in the XMEGA A manual.
 * Otherwise we have to do a switch case here.
 *
 * \param pin GPIO pin number. This number can be generated by the macro
 * CREATE_GPIO_PIN.
 * \return Pointer to the port on which the pin is located.
 */
static inline void *gpio_pin_to_port(gpio_pin_t pin)
{
	// Each port has an offset of 0x20
	return (void *)(PORTA_BASE + (pin >> 3) * 0x20);
}

/**
 * \brief Generate port pin mask form GPIO pin number
 *
 * \param pin GPIO pin number.
 * \return Pin mask.
 */
static inline pin_mask_t gpio_pin_to_mask(gpio_pin_t pin)
{
	return 1U << (pin & 0x7);
}

/**
 * \brief Select the GPIO function for a set of pins on a port
 *
 * \param port Pointer to the port
 * \param pin_mask Mask containing the pins that should be configured
 * \param flags Bitmask of flags specifying additional configuration
 * parameters.
 */
extern void port_select_gpio(void *port, pin_mask_t pin_mask,
		port_pin_flags_t flags);

/**
 * \brief Select the GPIO function for a single pin
 *
 * \param pin The pin to configure
 * \param flags Bitmask of flags specifying additional configuration
 * parameters.
 */
static inline void port_select_gpio_pin(gpio_pin_t pin, port_pin_flags_t flags)
{
	port_select_gpio(gpio_pin_to_port(pin), gpio_pin_to_mask(pin), flags);
}


/**
 * \internal
 * \weakgroup port_driver_internals_group
 * \ingroup port_driver_group
 * @{
 */

/**
 * \internal
 * \name PORT Pin Numbering
 *
 * These macros are used to generate GPIO pin numbers for each port with the
 * CREATE_GPIO_PIN macro. Each port has 8 pins so e.g. the first pin on PORTB
 * gets number 8, first pin on PORTC gets 16 ...
 */
//@{
#define PORT_PORTA        (0 * 8)
#define PORT_PORTB        (1 * 8)
#define PORT_PORTC        (2 * 8)
#define PORT_PORTD        (3 * 8)
#define PORT_PORTE        (4 * 8)
#define PORT_PORTF        (5 * 8)
#define PORT_PORTG        (6 * 8)
#define PORT_PORTH        (7 * 8)
#define PORT_PORTJ        (8 * 8)
#define PORT_PORTK        (9 * 8)
#define PORT_PORTL        (10 * 8)
#define PORT_PORTM        (11 * 8)
#define PORT_PORTN        (12 * 8)
#define PORT_PORTP        (13 * 8)
#define PORT_PORTQ        (14 * 8)
#define PORT_PORTR        (15 * 8)
//@}

__always_inline static void gpio_set_value_inline(gpio_pin_t pin, bool value)
{
	pin_mask_t pin_mask = gpio_pin_to_mask(pin);
	void *port = gpio_pin_to_port(pin);

	if (value)
		port_write_reg(port, OUTSET, pin_mask);
	else
		port_write_reg(port, OUTCLR, pin_mask);
}

__always_inline static bool gpio_get_value_inline(gpio_pin_t pin)
{
	return (port_read_reg(gpio_pin_to_port(pin), IN) >> (pin & 0x7)) & 1;
}

extern void gpio_set_value_noninline(gpio_pin_t pin, bool value);
extern bool gpio_get_value_noninline(gpio_pin_t pin);
//!@}


/**
 * \brief Drive a GPIO pin to a given state
 *
 * This function will only have an effect if \a pin is configured as
 * an output.
 *
 * \param pin A number identifying the pin to act on.
 * \param value The desired state of the pin. \a true means drive the
 * pin high (towards Vdd), while \a false means drive the pin low
 * (towards Vss).
 */
__always_inline static void gpio_set_value(gpio_pin_t pin, bool value)
{
	if (is_constant(pin))
		gpio_set_value_inline(pin, value);
	else
		gpio_set_value_noninline(pin, value);
}

/**
 * \brief Read the current state of a GPIO pin
 *
 * \param pin A number identifying the pin to read.
 * \retval true The pin is currently high (close to Vdd)
 * \retval false The pin is currently low (close to Vss)
 */
__always_inline static bool gpio_get_value(gpio_pin_t pin)
{
	if (is_constant(pin))
		return gpio_get_value_inline(pin);
	else
		return gpio_get_value_noninline(pin);
}

/**
 * \brief Toggle the current state of a GPIO pin
 * 
 * \param pin A number identifying the pin to act on.
 */
__always_inline static void gpio_toggle_value(gpio_pin_t pin)
{
	pin_mask_t pin_mask = gpio_pin_to_mask(pin);
	void *port = gpio_pin_to_port(pin);

	port_write_reg(port, OUTTGL, pin_mask);
}
//!@}


#endif /* GPIO_PORT_H_INCLUDED */
